local sk__chouxi = fk.CreateSkill {

  name = "sk__chouxi",

  tags = {},

}


sk__chouxi:addEffect("active", {
  name = "sk__chouxi",
  anim_type = "offensive",
  card_num = 1,
  target_num = 1,
  prompt = "#sk__chouxi",
  can_use = function(self, player)
    return player:usedSkillTimes(sk__chouxi.name, Player.HistoryPhase) == 0
  end,
  card_filter = function(self, player, to_select, selected)
    return #selected == 0 and not player:prohibitDiscard(to_select)
  end,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= player
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    room:throwCard(effect.cards, sk__chouxi.name, player, player)
    if player.dead then return end
    local cards = room:getNCards(3)
    room:moveCardTo(cards, Card.Processing, nil, fk.ReasonJustMove, sk__chouxi.name, nil, true, player)
    local types = { "basic", "trick", "equip" }
    local choices = {}
    for _, id in ipairs(cards) do
      local type = Fk:getCardById(id):getTypeString()
      table.removeOne(types, type)
      table.insertIfNeed(choices, type)
    end
    if #room:askForDiscard(target, 1, 1, false, sk__chouxi.name, true, ".|.|.|.|.|" .. table.concat(types, ","),
          "#sk__chouxi-discard:" .. player.id) > 0 then
      if not player.dead then
        room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonJustMove, sk__chouxi.name, nil, true, player)
      end
    else
      room:damage {
        from = player,
        to = target,
        damage = 1,
        skillName = sk__chouxi.name,
      }
      if target.dead then
        room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonJustMove, sk__chouxi.name, nil, true, player)
      else
        local _, choice = room:askToChooseCardsAndChoice(target, {
          cards = cards,
          choices = choices,
          skill_name = sk__chouxi.name,
          prompt = "#sk__chouxi-get:" .. player.id,
          cancel_choices = {},
          min = 0,
          max = 0,
          all_cards = cards
        })
        local obtain, discard = {}, {}
        for _, id in ipairs(cards) do
          if Fk:getCardById(id):getTypeString() == choice then
            table.insert(obtain, id)
          else
            table.insert(discard, id)
          end
        end
        room:moveCardTo(obtain, Card.PlayerHand, target, fk.ReasonJustMove, sk__chouxi.name, nil, true, target)
        if #discard > 0 then
          if player.dead then
            room:moveCardTo(discard, Card.DiscardPile, nil, fk.ReasonJustMove, sk__chouxi.name, nil, true, nil)
          else
            room:moveCardTo(discard, Card.PlayerHand, player, fk.ReasonJustMove, sk__chouxi.name, nil, true, player)
          end
        end
      end
    end
  end,
})

return sk__chouxi
